package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.FocusOnResumesDO;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.service.FocusOnResumesService;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.OrganizationRolesService;
import com.jinzhi.jzweb.utils.IdCardUtil;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <pre>
 * 关注简历
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/focusOnResumes")
public class FocusOnResumesController extends BaseController {
    @Autowired
    private FocusOnResumesService focusOnResumesService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private DictService dictService;
    @Autowired
    private OrganizationRolesService organizationRolesService;

    @GetMapping()
    @RequiresPermissions("jzweb:focusOnResumes:focusOnResumes")
    String FocusOnResumes(Model model) {
        //判断该企业是否已经有关注简历的信息，如果没有，则提示先关注
        List<FocusOnResumesDO> focusOnResumesDOS = focusOnResumesService.findByKv("org_id",this.getUser().getOrgId());
        if(focusOnResumesDOS != null && focusOnResumesDOS.size() > 0){
            model.addAttribute("state","true");
            FocusOnResumesDO focus = focusOnResumesService.selMaxTime(this.getUser().getOrgId());
            if(focus != null){
                model.addAttribute("focusRes",focus.getResidues());
            }else{
                model.addAttribute("focusRes",0);
            }
        }else{
            Integer focNum = 10;
            Map map = new HashMap();
            map.put("name", "关注简历免费次数");
            map.put("type", "focus_res");
            String focuson = dictService.selDicBy(map);
            if (StringUtils.isNotBlank(focuson)) {
                //判断是否为数字
                if (IdCardUtil.isNumeric(focuson)) {
                    focNum = Integer.parseInt(focuson);
                }
            }
            model.addAttribute("focusRes",focNum);
            model.addAttribute("state","false");
        }
        return "jzweb/focusOnResumes/focusOnResumes";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:focusOnResumes:focusOnResumes")
    public Result<Page<FocusOnResumesDO>> list(FocusOnResumesDO focusOnResumesDTO) {
        Wrapper<FocusOnResumesDO> wrapper = new EntityWrapper<FocusOnResumesDO>().orderBy("id", false);
        if (focusOnResumesDTO.getState() != null) {
            wrapper.eq("state", focusOnResumesDTO.getState());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(focusOnResumesDTO.getUserName())) {
            Wrapper<UserDO> Wrapper = new EntityWrapper<UserDO>().like("name", focusOnResumesDTO.getUserName());
            List<UserDO> userDOS = userService.selectList(Wrapper);
            String str = "";
            for (UserDO userDO : userDOS) {
                str = str + userDO.getId() + ",";
            }
            wrapper.and();
            if (org.apache.commons.lang3.StringUtils.isNotBlank(str)) {
                wrapper.in("user_id", str.substring(0, str.length() - 1));
            } else {
                wrapper.eq("user_id", null);
            }
        }
        String roleName = getRoleNames();//查询用户角色
        if (!roleName.contains("超级用户角色")) {
            wrapper.eq("org_id", this.getUser().getOrgId());
        } else {
            if (StringUtils.isNotBlank(focusOnResumesDTO.getOrgName())) {
                Wrapper<OrganizationNewsDO> orWrapper = new EntityWrapper<OrganizationNewsDO>().like("name", focusOnResumesDTO.getOrgName());
                List<OrganizationNewsDO> organizationNewsDOS = organizationNewsService.selectList(orWrapper);
                String str = "";
                for (OrganizationNewsDO organizationNewsDO : organizationNewsDOS) {
                    str = str + organizationNewsDO.getId() + ",";
                }
                wrapper.and();
                if (StringUtils.isNotBlank(str)) {
                    wrapper.in("org_id", str.substring(0, str.length() - 1));
                } else {
                    wrapper.eq("org_id", null);
                }
            }
        }

        Page<FocusOnResumesDO> page = focusOnResumesService.selectPage(getPage(FocusOnResumesDO.class), wrapper);
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            FocusOnResumesDO obj = (FocusOnResumesDO) page.getRecords().get(i);
            if (obj.getUserId() != null) {
                UserDO userDO = userService.selectById(obj.getUserId());
                if (userDO != null) {
                    obj.setUserName(userDO.getName());
                }
            }
            if (obj.getOrgId() != null) {
                OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(obj.getOrgId());
                if (organizationNewsDO != null) {
                    obj.setOrgName(organizationNewsDO.getName());
                }
            }
            list.add(obj);
        }
        page.setRecords(list);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:focusOnResumes:add")
    String add() {
        return "jzweb/focusOnResumes/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:focusOnResumes:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        FocusOnResumesDO focusOnResumes = focusOnResumesService.selectById(id);
        model.addAttribute("focusOnResumes", focusOnResumes);
        return "jzweb/focusOnResumes/edit";
    }

    @Log("添加关注简历")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:focusOnResumes:add")
    public Result<String> save(FocusOnResumesDO focusOnResumes) {
        focusOnResumesService.insert(focusOnResumes);
        return Result.ok();
    }

    @Log("修改关注简历")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:focusOnResumes:edit")
    public Result<String> update(FocusOnResumesDO focusOnResumes) {
        boolean update = focusOnResumesService.updateById(focusOnResumes);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除关注简历")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:focusOnResumes:remove")
    public Result<String> remove(Long id) {
        focusOnResumesService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除关注简历")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:focusOnResumes:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        focusOnResumesService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @Log("购买次数")
    @GetMapping("/payNum")
    @RequiresPermissions("jzweb:focusOnResumes:add")
    String payNum(Model model) {
        Double money = 10.00;
        Map<String, String> maps = new HashMap<>();
        maps.put("name", "关注金额");
        maps.put("type", "focuson_money");
        String topMoney = dictService.selDicBy(maps);
        if (StringUtils.isNotBlank(topMoney)) {
            //判断是否为数字或小数
            if (IdCardUtil.isNumericDou(topMoney)) {
                money = Double.parseDouble(topMoney);
            }
        }
        model.addAttribute("money", money);

        String roleName = getRoleNames();//查询用户角色
        if (!roleName.contains("超级用户角色")) {
            Map map = new HashMap();
            map.put("id", this.getUser().getOrgId());
            map.put("name", "--");
            if (this.getUser().getOrgId() != null) {
                OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(this.getUser().getOrgId());
                if (organizationNewsDO != null) {
                    map.put("name", organizationNewsDO.getName());
                }
            }
            model.addAttribute("type", 1);
            model.addAttribute("orgRole", map);
        } else {
            model.addAttribute("type", 0);
            model.addAttribute("orgRole", organizationRolesService.selByRoleType(3));
        }
        return "jzweb/focusOnResumes/payNum";
    }

}
